Parameterised Reporting of Predicted Antimicrobial Resistance Rates using Quarto




Daniel Weiand
Newcastle upon Tyne Hospitals NHS Foundation Trust

NHS-R and NHS.pycom conf (2023) | October 17

– Introduction –

Setting the scene

Medical Microbiology and Virology

  • ISO 15189 & UKAS accredited


  • 86 scientific and technical staff


  • 1,1+m samples per annum


  • £6+m annual budget

Background

The challenge

Task: Generate custom AMR reports


Stratifying by: Hospital, directorate, ward, age, etc.


Timeframe/frequency: Every quarter


Bacterium with an expression of existential dread

– ??Parameterised reports –

File with the word '.qmd' inside and the word 'Function' above.

An arrow points from 'Input' with 'params$year' to the previous image with 'Function' and '.qmd' file.

In addition to the previous two images, arrows point to five reports with years 2019 through 2023 on them in a flow chart.

– Further information –

Kudos

Jadey N Ryan gave an excellent talk on creating parameterised reports at this year’s posit::conf(2023)

tinyurl.com/quarto-params

github.com/jadeynryan


Megan Hall wrote an excellent blog post on creating parameterised reports (2022)

https://meghan.rbind.io/blog/quarto-pdfs

https://github.com/meghall06

Reprex

Producing parameterised reports using the {nycflights13} dataset


github.com/send2dan/public

QR code leading to https://github.com/send2dan/public

– Project setup –

Requirements

  1. Quarto script with YAML specifying parameter(s)
  1. Code to wrangle dataset into “params”
  1. +/- Specification of parameter(s) in body of report
  1. +/- R script to execute/render parameterised reports using Quarto
  1. +/- Inline code

YAML

---
title: "Parameterised report"                         # Metadata
subtitle: "Analysis of the {nycflights13} dataset"
author: "Daniel Weiand"
format:                             # Set format types
  html:                                     
  docx:
  revealjs: 
params:                             # Set default parameter key-value pairs
  origin: "NA" 
  dest: "NA"                  
  month: "NA"    
---
    
Report content goes here.           # Write content

Wrangling data into “params”

For each parameter, wrangle dataset into “params” after loading required packages and raw data before any additional data wrangling steps.

library(nycflights13)                                       # load packages and data
flights <- nycflights13::flights

if (!is.na(params$origin) && params$origin != "NA") {       # Param for nycflights13::flights$origin
  flights <- flights[flights$origin == params$origin, ]
}
if (nrow(flights) == 0) {
  stop("Invalid selection. Did you misspell `origin`?")
}

not_cancelled <- flights |>                                 # undertake additional wrangling steps
  filter(!is.na(arr_delay), !is.na(dep_delay)) 
    
---
Report content goes here.                                   # Write content

Specification parameters in report

This report focuses on the following parameter(s):

Origin airport: **`r params$origin`**             # Param for flights$origin

Destination airport: **`r params$dest`**          # Param for flights$dest

Month of flight departure: **`r params$month`**   # Param for flights$month

Inline code

Strengthen the narrative of your report by including executable expressions within markdown using `r `.

For example, we can use inline code to state the number of observations in our data.


There are `r nrow(flights)` observations in our data. 


There are 336776 observations in our data.

– Rendering reports –

Three options for rendering reports


  1. YAML


  1. Terminal


  1. quarto::quarto_render() function

Rendering reports using YAML

Manually change the parameter values in the header YAML of your .qmd file and hit Render.

params:
  dest: "NA"
  origin: "JFK"             # JFK airport specified in YAML
  month: "NA"

Rendering reports using the terminal

Use the terminal to specify the parameter(s) you want to use.

quarto render 
quarto render parameterised_report.qmd 
quarto render parameterised_report.qmd -P 
quarto render parameterised_report.qmd -P origin:JFK      # JFK specified as origin airport

Rendering reports using quarto::quarto_render()

Leverage the quarto::quarto_render() function in R to automatically create reports for each parameter subcategory and name each file as specified.

runparams <- function(origin) {             # create function
  quarto::quarto_render(
    "parameterised_report.qmd",                       # Specify .qmd file name
    output_format = "html",                           # Specify output format
    execute_params = list(origin = origin),           # Specify parameter of interest
    output_file = glue::glue("report_{origin}.html")  # Specify (parameterised) file name
  )
}

runparams(origin = "JFK")                 # code to run a single report using function

params_origin <- unique(flights$origin)             # Or execute code with purrr:map() function
purrr::map(params_origin, runparams)

– Apply theory in practice –

Aim

To model predicted drug resistance rates using the AMR package for R.


Methods

The LIMS was interrogated to collect data on all culture-positive blood cultures collected between 01 April 2019 and 31 July 2023 .

The AMR package for R

The AMR package [1,2] is a free, open-source and independent package for R [3] that provides a standard for clean and reproducible analysis and prediction of Antimicrobial Resistance (AMR).


Helps to: cleverly de-duplicate data; calculate and visualise historical AMR rates; and predict future AMR rates using regression models.

Other key packages

  • The {NHSRplotthedots} package [4] is used to plot time series data and detect special cause variation by applying statistical process control (SPC) rules.


  • The {ggsurvfit} and {survival} packages [57] are used to conduct survival analyses.

– Results –

Totals

Since Q1 2019:


11967 distinct positive blood cultures


Collected from 7435 distinct patients


Leading to isolation of 13227 organisms

Distinct positive blood cultures per month

Survival analysis

Characteristic 90-day survival (95% CI) 180-day survival (95% CI) 365-day survival (95% CI)
Overall 88% (87%, 89%) 86% (85%, 87%) 84% (83%, 85%)
Gram stain result
    Gram-negative 87% (86%, 88%) 84% (83%, 86%) 82% (81%, 84%)
    Gram-positive 89% (88%, 90%) 88% (87%, 89%) 85% (84%, 86%)
Organism isolated
    E. coli 88% (86%, 90%) 86% (84%, 88%) 84% (82%, 86%)
    K. pneumoniae 85% (82%, 88%) 82% (79%, 86%) 79% (75%, 83%)
    Other 89% (88%, 90%) 87% (86%, 88%) 85% (84%, 86%)
    P. aeruginosa 83% (77%, 89%) 80% (74%, 86%) 79% (74%, 86%)
    S. aureus 85% (82%, 88%) 83% (80%, 87%) 81% (77%, 84%)

Organism-specific historical AMR rates

Organism-specific predicted AMR rates

– Parameterised results –

By hospital

By ward

By ward

By age group

Output

Sharing the results


Benefits of parameterised reporting of AMR rates

Informs clinical decision-making

Helps target (limited) infection prevention and control resources

Assists with updating of antimicrobial guidelines

Helps justify investment in antimicrobial stewardship

– Thanks for listening –

Further information

Daniel Weiand, Consultant medical microbiologist

Newcastle upon Tyne Hospitals NHS Foundation Trust

Email: dweiand@nhs.net

NHS-R community blog: https://nhsrcommunity.com/author/daniel-weiand/

send2dan

github.com/send2dan/public

QR code leading to https://github.com/send2dan/public

References

1
Berends MS, Luz CF, Friedrich AW, et al. AMR: An R package for working with antimicrobial resistance data. Journal of Statistical Software 2022;104:1–31. doi:10.18637/jss.v104.i03
2
Berends MS, Luz CF, Souverein D, et al. AMR: Antimicrobial resistance data analysis. 2023. https://CRAN.R-project.org/package=AMR
3
R Core Team. R: A language and environment for statistical computing. Vienna, Austria: : R Foundation for Statistical Computing 2022. https://www.R-project.org/
4
Reading C, Wellesley-Miller S, Turner Z, et al. NHSRplotthedots: Draw XmR charts for NHSE/i making data count programme. 2021. https://nhs-r-community.github.io/NHSRplotthedots/
5
Sjoberg DD, Baillie M, Fruechtenicht C, et al. Ggsurvfit: Flexible time-to-event figures. 2023. https://CRAN.R-project.org/package=ggsurvfit
6
Therneau TM. Survival: Survival analysis. 2022. https://github.com/therneau/survival
7
Terry M. Therneau, Patricia M. Grambsch. Modeling survival data: Extending the Cox model. New York: : Springer 2000.